#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Project: spd-sxmcc
"""
@author: lyndon
@time Created on 2018/10/12 9:50
@desc
"""

import re, urllib2

fp = open("xicidaili.txt", 'r')
lines = fp.readlines()

with open('checked_success_ip.txt', 'w')as f:
    for ip in lines:
        try:
            ip = ip.split('\t')[0]
            print("当前代理IP " + ip)
            proxy = urllib2.ProxyHandler({'http': ip})
            opener = urllib2.build_opener(proxy)
            urllib2.install_opener(opener)

            url = "http://www.baidu.com"
            request = urllib2.Request(url)
            request.add_header('User-Agent', 'fake-client')
            response = urllib2.urlopen(request, timeout=5)

            f.write(ip + '\n')
            f.flush()
            # print(response.read())

            # opener = urllib.request.build_opener(proxy, urllib.request.HTTPHandler)
            # urllib.request.install_opener(opener)
            # data = urllib.request.urlopen(url).read().decode('utf-8', 'ignore')
            print("通过")

            print("-----------------------------")
        except Exception as err:
            print(err)
            print("-----------------------------")

    fp.close()
